package vip.zhaotao.utils;

import freemarker.template.TemplateException;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

/**
 * Word工具类
 */
public class WordUtils {

    /**
     * 生成Word docx格式文件
     *
     * @param outputStream 输出流
     * @param dataModel    数据
     * @throws IOException
     * @throws TemplateException
     */
    public static void generateDocxFile(OutputStream outputStream,
                                        Object dataModel) throws IOException, TemplateException {
        FileUtils.generateFile(
                outputStream,
                dataModel,
                "template/word",
                "word.docx",
                "word",
                "document.xml",
                "document.ftl"
        );
    }

    /**
     * 生成Word doc格式文件
     *
     * @param outputStream
     * @param dataModel
     * @throws IOException
     * @throws TemplateException
     */
    public static void generateDocFile(OutputStream outputStream,
                                       Object dataModel) throws IOException, TemplateException {
        String resourcePath = FileUtils.getResourcePath("template/word");
        FreeMarkerUtils.templateProcess(
                dataModel,
                new OutputStreamWriter(outputStream),
                resourcePath,
                "doc.ftl",
                FileUtils.utf8().name()
        );
    }
}
